package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysSchool;
import com.ruoyi.system.service.ISysSchoolService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 学校管理信息Controller
 *
 * @author ruoyi
 * @date 2025-09-28
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/system/school")
public class SysSchoolController extends BaseController {

    private final ISysSchoolService sysSchoolService;

    @Anonymous
    @GetMapping("/list")
    public TableDataInfo list(SysSchool sysSchool) {
        startPage();
        List<SysSchool> list = sysSchoolService.selectSysSchoolList(sysSchool);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('system:school:export')")
    @Log(title = "学校管理信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysSchool sysSchool) {
        List<SysSchool> list = sysSchoolService.selectSysSchoolList(sysSchool);
        ExcelUtil<SysSchool> util = new ExcelUtil<SysSchool>(SysSchool.class);
        util.exportExcel(response, list, "学校管理信息数据");
    }

    @PreAuthorize("@ss.hasPermi('system:school:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(sysSchoolService.getById(id));
    }

    @PreAuthorize("@ss.hasPermi('system:school:add')")
    @Log(title = "学校管理信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysSchool sysSchool) {
        return toAjax(sysSchoolService.save(sysSchool));
    }

    @PreAuthorize("@ss.hasPermi('system:school:edit')")
    @Log(title = "学校管理信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysSchool sysSchool) {
        return toAjax(sysSchoolService.updateById(sysSchool));
    }

    @PreAuthorize("@ss.hasPermi('system:school:remove')")
    @Log(title = "学校管理信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sysSchoolService.removeBatchByIds(Arrays.asList(ids)));
    }
}
